<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备号" prop="devObj">
      <el-input v-model="dataForm.devObj" placeholder="设备号"></el-input>
    </el-form-item>
    <el-form-item label="累计发电总量" prop="devElectriccount">
      <el-input v-model="dataForm.devElectriccount" placeholder="累计发电总量"></el-input>
    </el-form-item>
    <el-form-item label="光总发电量（度）" prop="devLightelectriccount">
      <el-input v-model="dataForm.devLightelectriccount" placeholder="光总发电量（度）"></el-input>
    </el-form-item>
    <el-form-item label="风机输入总发电量（度）" prop="devFaninelectriccount">
      <el-input v-model="dataForm.devFaninelectriccount" placeholder="风机输入总发电量（度）"></el-input>
    </el-form-item>
    <el-form-item label="累计更换成本（元）" prop="devReplacecostcount">
      <el-input v-model="dataForm.devReplacecostcount" placeholder="累计更换成本（元）"></el-input>
    </el-form-item>
    <el-form-item label="累计发电时间" prop="devElectrictimecount">
      <el-input v-model="dataForm.devElectrictimecount" placeholder="累计发电时间"></el-input>
    </el-form-item>
    <el-form-item label="累计故障时间（小时）" prop="devFaulthourcount">
      <el-input v-model="dataForm.devFaulthourcount" placeholder="累计故障时间（小时）"></el-input>
    </el-form-item>
    <el-form-item label="故障率（%)" prop="devFailtrate">
      <el-input v-model="dataForm.devFailtrate" placeholder="故障率（%)"></el-input>
    </el-form-item>
    <el-form-item label="年发电总量（度）" prop="devElectricyearcount">
      <el-input v-model="dataForm.devElectricyearcount" placeholder="年发电总量（度）"></el-input>
    </el-form-item>
    <el-form-item label="当年收益（元）" prop="devYearprofitcount">
      <el-input v-model="dataForm.devYearprofitcount" placeholder="当年收益（元）"></el-input>
    </el-form-item>
    <el-form-item label="当年碳排放( 公斤 )" prop="devCarbondischargeyear">
      <el-input v-model="dataForm.devCarbondischargeyear" placeholder="当年碳排放( 公斤 )"></el-input>
    </el-form-item>
    <el-form-item label="当年更换成本（元）" prop="devYearreplacecostcount">
      <el-input v-model="dataForm.devYearreplacecostcount" placeholder="当年更换成本（元）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          devId: 0,
          devObj: '',
          devElectriccount: '',
          devLightelectriccount: '',
          devFaninelectriccount: '',
          devReplacecostcount: '',
          devElectrictimecount: '',
          devFaulthourcount: '',
          devFailtrate: '',
          devElectricyearcount: '',
          devYearprofitcount: '',
          devCarbondischargeyear: '',
          devYearreplacecostcount: ''
        },
        dataRule: {
          devObj: [
            { required: true, message: '设备号不能为空', trigger: 'blur' }
          ],
          devElectriccount: [
            { required: true, message: '累计发电总量不能为空', trigger: 'blur' }
          ],
          devLightelectriccount: [
            { required: true, message: '光总发电量（度）不能为空', trigger: 'blur' }
          ],
          devFaninelectriccount: [
            { required: true, message: '风机输入总发电量（度）不能为空', trigger: 'blur' }
          ],
          devReplacecostcount: [
            { required: true, message: '累计更换成本（元）不能为空', trigger: 'blur' }
          ],
          devElectrictimecount: [
            { required: true, message: '累计发电时间不能为空', trigger: 'blur' }
          ],
          devFaulthourcount: [
            { required: true, message: '累计故障时间（小时）不能为空', trigger: 'blur' }
          ],
          devFailtrate: [
            { required: true, message: '故障率（%)不能为空', trigger: 'blur' }
          ],
          devElectricyearcount: [
            { required: true, message: '年发电总量（度）不能为空', trigger: 'blur' }
          ],
          devYearprofitcount: [
            { required: true, message: '当年收益（元）不能为空', trigger: 'blur' }
          ],
          devCarbondischargeyear: [
            { required: true, message: '当年碳排放( 公斤 )不能为空', trigger: 'blur' }
          ],
          devYearreplacecostcount: [
            { required: true, message: '当年更换成本（元）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.devId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.devId) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmdevice/info/${this.dataForm.devId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.devObj = data.r5rmDevice.devObj
                this.dataForm.devElectriccount = data.r5rmDevice.devElectriccount
                this.dataForm.devLightelectriccount = data.r5rmDevice.devLightelectriccount
                this.dataForm.devFaninelectriccount = data.r5rmDevice.devFaninelectriccount
                this.dataForm.devReplacecostcount = data.r5rmDevice.devReplacecostcount
                this.dataForm.devElectrictimecount = data.r5rmDevice.devElectrictimecount
                this.dataForm.devFaulthourcount = data.r5rmDevice.devFaulthourcount
                this.dataForm.devFailtrate = data.r5rmDevice.devFailtrate
                this.dataForm.devElectricyearcount = data.r5rmDevice.devElectricyearcount
                this.dataForm.devYearprofitcount = data.r5rmDevice.devYearprofitcount
                this.dataForm.devCarbondischargeyear = data.r5rmDevice.devCarbondischargeyear
                this.dataForm.devYearreplacecostcount = data.r5rmDevice.devYearreplacecostcount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmdevice/${!this.dataForm.devId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'devId': this.dataForm.devId || undefined,
                'devObj': this.dataForm.devObj,
                'devElectriccount': this.dataForm.devElectriccount,
                'devLightelectriccount': this.dataForm.devLightelectriccount,
                'devFaninelectriccount': this.dataForm.devFaninelectriccount,
                'devReplacecostcount': this.dataForm.devReplacecostcount,
                'devElectrictimecount': this.dataForm.devElectrictimecount,
                'devFaulthourcount': this.dataForm.devFaulthourcount,
                'devFailtrate': this.dataForm.devFailtrate,
                'devElectricyearcount': this.dataForm.devElectricyearcount,
                'devYearprofitcount': this.dataForm.devYearprofitcount,
                'devCarbondischargeyear': this.dataForm.devCarbondischargeyear,
                'devYearreplacecostcount': this.dataForm.devYearreplacecostcount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
